import React, { Component } from 'react';
import {Layout, Button, Tooltip, Select, Input, Row, Col, Table, Form, Modal, message, DatePicker,Space} from 'antd';
import '../App.css';
import {ExclamationCircleOutlined, SearchOutlined} from '@ant-design/icons';
import Highlighter from 'react-highlight-words';
import { withRouter } from "react-router-dom";
import '../pages/webExcel.css'
import {findAccountReports,duplicateCheck,removeAccountReport} from "../api/report_table";
import {delUser} from "../api/Security";
const { Header, Content, Footer } = Layout;
const { Option } = Select;

 class Account_report extends Component {
    constructor(props) {
        super(props)
        this.state = {
            data:[],  //财务报表列表
            workbookList:[], //报表类型 如 资产负债表 损益表等
            setList:[],
            isModalVisible:false,  //新增报表Modal
            filterReportTypes:[]    //过滤报表类别
        }
    }

    componentDidMount() {
        findAccountReports().then(
            (res) => {
                let _filterReportTypes=[];
                res.data.workbookList.forEach(function(val, index){
                    _filterReportTypes.push({
                        text: val.name,
                        value: val.name,
                    },)
                });


                this.setState({
                        data: res.data.reportList,
                        workbookList:res.data.workbookList,
                        setList:res.data.setList,
                        filterReportTypes:_filterReportTypes
                })
            },
            (error) => {
                console.log("get role List failed!");
            }
        );
    }


     showModal = () => {
         this.setState({isModalVisible:true});
    };
     accountReportTemplate;

//新建报表
     onFinish = (values) => {
         console.log(values)
         duplicateCheck(values.accountReportTemplate,values.accountSet,values.year,values.month).then(
             (res) => {
                    if(res.data==false){
                        message.error("该报表已经存在，不能重复生成")
                    }else{
                        this.setState({isModalVisible:false});
                        this.props.history.push("/viewReport/"+values.accountReportTemplate+"/"+
                            values.accountSet+"/"+values.year+"/"+values.month+"/"
                            +values.orgName+"/"+values.reportDate+"/"+values.reportNo
                            +"/"+values.lister+"/"+values.auditor+"/1/-1"
                        );
                    }
             },
             (error) => {
                 console.log("get role List failed!");
             }
         );
    };

     onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };

     //显示报表
     showReport=(id)=>{
               //跳转到报表
         this.props.history.push("/viewReport/1/1/1/1/1/1/1/1/1/0/"+id)



     }

     deleteReport=(id)=>{
         const { confirm } = Modal;
         confirm({
             title: 'Do you Want to delete this items',
             icon: <ExclamationCircleOutlined />,
             content: '删除选中的报表',
             onOk() {
                 removeAccountReport(id).then(
                     (res) => {
                         if(res.msg=="操作成功"){
                             message.success("删除成功");
                             //todo  应该改为删除前端数据  xj 2021-11-11
                             window.location.reload();
                         }else{
                             message.error("删除失败")
                         }
                     },
                     (error) => {
                         message.error("删除失败")
                     }
                 )
             },
             onCancel() {
                 console.log('Cancel');

             },
         });
     }

    render() {
        const columns = [
            {
                title: '类别',
                dataIndex: 'templateName',
                key: 'templateName',
                filters: this.state.filterReportTypes,
                onFilter: (value, record) => record.templateName.indexOf(value) === 0,
                sorter: (a, b) => a.templateName - b.templateName,
                sortDirections: ['descend'],
            },
            {
                title: '账套',
                dataIndex: 'accountSet',
                key: 'accountSet',
                // filters: [
                //     {
                //         text: '测试账套1',
                //         value: '测试账套1',
                //     }
                // ],
                // onFilter: (value, record) => record.aaccountSetName.indexOf(value) === 0,
                sorter: (a, b) => a.aaccountSetName - b.aaccountSetName,
                defaultSortOrder: 'descend',
            },


            {
                title: '单位',
                dataIndex: 'orgName',
                key: 'orgName',
            },


            {
                title: '报表编号',
                dataIndex: 'reportNo',
                key: 'reportNo',
            },

            {
                title: '报表日期',
                dataIndex: 'reportDate',
                key: 'reportDate',
                defaultSortOrder: 'descend',
                sorter: (a, b) => a.reportDate - b.reportDate,
            },

            {
                title: '编制人',
                dataIndex: 'lister',
                key: 'lister',
            },
            {
                title: '审批人',
                dataIndex: 'auditor',
                key: 'auditor',
            },
            // {
            //     title: '年',
            //     dataIndex: 'year',
            //     key: 'year',
            //     defaultSortOrder: 'descend',
            //     sorter: (a, b) => a.year - b.year,
            // },
            // {
            //     title: '月',
            //     dataIndex: 'month',
            //     key: 'month',
            //     defaultSortOrder: 'descend',
            //     sorter: (a, b) => a.month - b.month,
            //
            // },

            {
                title: '操作',
                dataIndex: 'id',
                key: 'id',
                render: (id) =><Space><Button type="link" onClick={()=>this.showReport(id)}>查看</Button><Button  type="link" onClick={()=>this.deleteReport(id)}>删除</Button></Space>,
            },



        ];


        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                //console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            },
            onSelect: (record, selected, selectedRows) => {
                //因为删除了一项啊 所以这里要减去！
                // if(selectedRows[0]==undefined){
                //     selectedRows.splice(0,1)
                // }
                // this.props.selectRowsMethod(selectedRows);
             },
            onSelectAll: (selected, selectedRows, changeRows) => {
              //  console.log(selected, selectedRows, changeRows);
            },
        };


        const handleCancel = () => {
            this.setState({isModalVisible:false});
        };


        function handleChange(value) {
            console.log(`selected ${value}`);
        }

        return (
            <>


                    <Button onClick={this.showModal}>新建报表</Button>


                    <Table
                    columns={columns}
                    // rowSelection={{ ...rowSelection}}
                    dataSource={this.state.data}
                   // defaultExpandAll = {true}
                   // defaultExpandAllRows={this.state.expandAllRowsFlag}
             />


                <Modal title="新增报表" visible={this.state.isModalVisible}
                       //onOk={handleOk}
                       onCancel={handleCancel}
                footer={null}
                >

                    <Form
                        name="basic"
                        labelCol={{
                            span: 8,
                        }}
                        wrapperCol={{
                            span: 16,
                        }}
                        initialValues={{
                            remember: true,
                        }}
                        onFinish={this.onFinish}
                        onFinishFailed={this.onFinishFailed}
                        autoComplete="off"
                    >
                        <Form.Item
                            label="报表类别"
                            name="accountReportTemplate"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Select  style={{ width: 120 }} onChange={handleChange}>
                                {
                                    this.state.workbookList.map((item,index)=>{
                                        return (
                                            <Option value={ item.id } key={ item.key }>{ item.name }</Option>
                                        )
                                    })
                                }
                            </Select>
                        </Form.Item>

                        <Form.Item
                            label="账套"
                            name="accountSet"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Select style={{ width: 120 }} onChange={handleChange}>
                                {
                                    this.state.setList.map((item,index)=>{
                                        return (
                                            <Option value={ item.setid } key={ item.key }>{ item.fname }</Option>
                                        )
                                    })
                                }
                            </Select>
                        </Form.Item>


                        <Form.Item
                            label="年度"
                            name="year"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Select  style={{ width: 120 }} onChange={handleChange}>
                                <Option value="2019">2019</Option>
                                <Option value="2020">2020</Option>
                                <Option value="2021">2021</Option>
                                <Option value="2022">2022</Option>
                                <Option value="2023">2023</Option>
                                <Option value="2024">2024</Option>
                                <Option value="2025">2025</Option>
                                <Option value="2026">2026</Option>
                                <Option value="2027">2027</Option>
                                <Option value="2028">2028</Option>
                                <Option value="2029">2029</Option>
                                <Option value="2030">2030</Option>
                            </Select>
                        </Form.Item>



                        <Form.Item
                            label="月份"
                            name="month"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Select  style={{ width: 120 }} onChange={handleChange}>
                                <Option value="01">01</Option>
                                <Option value="02">02</Option>
                                <Option value="03">03</Option>
                                <Option value="04">04</Option>
                                <Option value="05">05</Option>
                                <Option value="06">06</Option>
                                <Option value="07">07</Option>
                                <Option value="08">08</Option>
                                <Option value="09">09</Option>
                                <Option value="10">10</Option>
                                <Option value="11">11</Option>
                                <Option value="12">12</Option>
                            </Select>
                        </Form.Item>

                        <Form.Item
                            label="单位"
                            name="orgName"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Input/>
                        </Form.Item>

                        <Form.Item
                            label="报表日期"
                            name="reportDate"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <DatePicker/>
                        </Form.Item>

                        <Form.Item
                            label="报表编号"
                            name="reportNo"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Input/>
                        </Form.Item>

                        <Form.Item
                            label="制表人"
                            name="lister"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Input/>
                        </Form.Item>


                        <Form.Item
                            label="审批人"
                            name="auditor"
                            rules={[
                                {
                                    required: true,
                                },
                            ]}
                        >
                            <Input/>
                        </Form.Item>

                        <Form.Item
                            wrapperCol={{
                                offset: 8,
                                span: 16,
                            }}
                        >
                            <Button type="primary" htmlType="submit">
                                生成报表
                            </Button>
                        </Form.Item>
                    </Form>
                </Modal>


            </>
        )
    }
}
export default withRouter(Account_report)
